// pages/contactsEdit/contactsEdit.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    readOnly:false,
    isBack:false,
    sex:0,
    birthDay:'',
    clientId:'',
    clientName:'',
    sexOption:['男','女']
  },

  chooseClient(){
    wx.navigateTo({
      url: '../chooseClient/chooseClient',
    })
  },
    /**日期选择 */
  bindChange(e){
    this.setData({
      birthDay: e.detail.value
    })
  },
  onSexChange(event){
    this.setData({
      sex: event.detail,
    });
  },

  /**提交 */
  commit(e){
    let that = this;
    let para = e.detail.value;
    para.khid=this.data.clientId;
    para.sex = this.data.sexOption[this.data.sex] =='未知'?'':this.data.sexOption[this.data.sex];
    para.birthDay=this.data.birthDay?this.data.birthDay:'';
    para.id=this.data.info.id?this.data.info.id:'';
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    api.curlPost('?cla=clientAppClass&fun=staffEdit',para).then((res)=>{
      prevPage.setData({
        khid:that.data.clientId
      })
      wx.navigateBack({
        delta:1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    let khid = options.khid;
    let companyName = options.clientName;
    if (!api.isBlank(companyName)) {
      this.setData({
        // readOnly:true,
        clientName:companyName,
        clientId:khid,
      })
    }
   
    let that = this;
    if(!api.isBlank(id)){
      api.curlPost('?cla=clientAppClass&fun=detailStaff',{khid:khid,id:id}).then((res)=>{
        let sex = 2;
        that.data.sexOption.forEach((v,i)=>{
          if(v == res.data.sex){
            sex = i;
          }
        })
        that.setData({
          info:res.data,
          sex:sex,
          birthDay:res.data.birthDay
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})